//
//  CPView.swift
//  ComponentUtils
//
//  Created by wrs on 2018/7/18.
//  Copyright © 2018年 wrs. All rights reserved.
//

import UIKit

public func getXIBView(xibName: String) -> UIView {
    let view = Bundle.main.loadNibNamed(xibName, owner: nil, options: nil)?.first as! UIView
    return view
}

extension UIView {
    @IBInspectable var cornerRadius: CGFloat {
        set {
            self.layer.cornerRadius = newValue
        }
        get {
            return self.layer.cornerRadius
        }
    }
    @IBInspectable var borderWidth: CGFloat {
        set {
            self.layer.borderWidth = newValue
        }
        get {
           return self.layer.borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        set {
            if let color = newValue {
                self.layer.borderColor = color.cgColor
            }
        }
        get {
            if let cgColor = self.layer.borderColor {
                 return UIColor(cgColor: cgColor)
            } else {
                return nil
            }
        }
    }
    var viewController: UIViewController? {
        get {
            var n = self.next
            while n != nil {
                if n is UIViewController {
                    return n as? UIViewController
                }
                n = n?.next
            }
            return nil
        }
    }
    
    var tableView: UITableView? {
        get {
            var n = self.next
            while n != nil {
                if n is UITableView {
                    return n as? UITableView
                }
                n = n?.next
            }
            return nil
        }
    }
    

    
    // MARK: -  水平平均布局子视图
    func constrainSubViewsForHorizontalAverage(subViewArray: [UIView]) -> Void {
        if subViewArray.count == 0 {
            return
        } else {
            for (index, view) in subViewArray.enumerated() {
                if let superView = view.superview {
                    if superView != self {
                        view.removeFromSuperview()
                        self.addSubview(view)
                    }
                } else {
                     self.addSubview(view)
                }
                view.translatesAutoresizingMaskIntoConstraints = false
                let top = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: CGFloat(1.0), constant: CGFloat(0))
                let bottom = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: CGFloat(1.0), constant: CGFloat(0))
                var left: NSLayoutConstraint
                if index == 0 {
                    left = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: CGFloat(1.0), constant: CGFloat(0))
                } else {
                    let multiplier = Double(index) / Double(subViewArray.count)
                    left = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: CGFloat(multiplier), constant: CGFloat(0))
                }
                let width = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.width, multiplier: CGFloat(Double(1)/Double(subViewArray.count)), constant: CGFloat(0))
                self.addConstraints([top, bottom, left, width])
            }
        }
    }
    
    // MARK: -  垂直平均布局子视图
    func constrainSubViewsForVerticalAverage(subViewArray: [UIView]) -> Void {
        if subViewArray.count == 0 {
            return
        } else {
            for (index, view) in subViewArray.enumerated() {
                if let superView = view.superview {
                    if superView != self {
                        view.removeFromSuperview()
                        self.addSubview(view)
                    }
                } else {
                    self.addSubview(view)
                }
                view.translatesAutoresizingMaskIntoConstraints = false
                let left = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: CGFloat(1.0), constant: CGFloat(0))
                self.addConstraint(left)
                let right = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: CGFloat(1.0), constant: CGFloat(0))
                self.addConstraint(right)
                var top: NSLayoutConstraint
                if index == 0 {
                    top = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: CGFloat(1.0), constant: CGFloat(0))
                } else {
                    top = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: subViewArray[index - 1], attribute: NSLayoutConstraint.Attribute.bottom, multiplier: CGFloat(1.0), constant: CGFloat(0))
                }
                self.addConstraint(top)
                let height = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.height, multiplier: CGFloat(Double(1)/Double(subViewArray.count)), constant: CGFloat(0))
               self.addConstraint(height)
            }
        }
    }
    
    // MARK: -  垂直自动布局子视图
    func constrainSubViewsForVerticalAuto(subViewArray: [UIView]) -> Void {
        if subViewArray.count == 0 {
            return
        } else {
            for (index, view) in subViewArray.enumerated() {
                if let superView = view.superview {
                    if superView != self {
                        view.removeFromSuperview()
                        self.addSubview(view)
                    }
                } else {
                    self.addSubview(view)
                }
                view.translatesAutoresizingMaskIntoConstraints = false
                let left = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.left, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.left, multiplier: CGFloat(1.0), constant: CGFloat(0))
                self.addConstraint(left)
                let right = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.right, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.right, multiplier: CGFloat(1.0), constant: CGFloat(0))
                self.addConstraint(right)
                var top: NSLayoutConstraint
                if index == 0 {
                    top = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.top, multiplier: CGFloat(1.0), constant: CGFloat(0))
                } else {
                    top = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: subViewArray[index - 1], attribute: NSLayoutConstraint.Attribute.bottom, multiplier: CGFloat(1.0), constant: CGFloat(0))
                }
                self.addConstraint(top)
                if index == subViewArray.count - 1 {
                    let bottom = NSLayoutConstraint(item: view, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: CGFloat(1.0), constant: CGFloat(0))
                    self.addConstraint(bottom)
                }
            }
        }
    }
    
    
    func setBorderCorner(borderWidth: CGFloat, borderColor: UIColor?, cornerRadius: CGFloat) -> Void {
        self.layer.cornerRadius = cornerRadius
        self.layer.borderColor = borderColor?.cgColor
        self.layer.borderWidth = borderWidth
        self.layer.masksToBounds = true
        self.clipsToBounds = true
    }
    
    func clearSubViews() -> Void {
        for subView in self.subviews {
            subView.removeFromSuperview()
        }
    }
    
   public class func getXIBView() -> UIView {
        let xibName = getClassName()
        let view = Bundle.main.loadNibNamed(xibName, owner: nil, options: nil)?.first as! UIView
        return view
    }
}
